﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SgLib;

public class ObstacleController : MonoBehaviour
{
    public bool isAttackable;
    [HideInInspector]
    public GameObject player;
    [HideInInspector]
    public Transform thisTrans;
    private Animator anim;
    private bool isDied = false;

    public static event System.Action KillYourTeam;
    public static event System.Action HitYourTeam;

    void OnEnable()
    {
        GameManager.GameStateChanged += GameManager_GameStateChanged;      
    }

    void OnDisable()
    {
        GameManager.GameStateChanged -= GameManager_GameStateChanged;
    }

    void GameManager_GameStateChanged(GameState newState, GameState oldState)
    {
        if (newState == GameState.Playing)
        {
            Destroy(gameObject);
        }
        else if (newState == GameState.PreGameOver)
        {
            // Before game over, i.e. game potentially will be recovered
        }
        else if (newState == GameState.GameOver)
        {
          
        }
    }
    // Use this for initialization
    void Start()
    {
        thisTrans = this.transform;
        player = GameObject.FindObjectOfType<PlayerController>().gameObject;
        anim = GetComponent<Animator>();
    }
	
    // Update is called once per frame
    void Update()
    {
        if (player && isAttackable && !isDied)
            Attack();
        if (isDied == true)
        {
            Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
        }

    }

    public void TakeDamage()
    {
        isDied = true;
        anim.Play("Obstacle_die");

        if (isAttackable == false)
        {
            KillYourTeam();
            if (SoundManager.Instance.gameOver != null)
            {
                SoundManager.Instance.PlaySound(SoundManager.Instance.gameOver);
            }
        }
        else
        {
            ScoreManager.Instance.AddScore(1);
            Collider2D[] colide = GetComponents<Collider2D>();
            foreach (Collider2D c in colide)
            {
                c.enabled = false;
                this.GetComponent<Rigidbody2D>().isKinematic = true;
            }

            if (SoundManager.Instance.get_hit != null)
                SoundManager.Instance.PlaySound(SoundManager.Instance.get_hit);
        }
    }

    void Attack()
    {
        if (GameManager.Instance.GameState == GameState.Playing)
        {

            if (player.transform.position.x > this.transform.position.x + 0.5f)
            {
                transform.position = Vector3.Lerp(thisTrans.position, player.transform.position, 0.3f);
                transform.localRotation = Quaternion.Euler(0, 180, 0);
            }

        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            if (isAttackable == false)
            {
                HitYourTeam();
                isDied = true;
                anim.Play("Obstacle_die");
            }
            else
            {
                SoundManager.Instance.PlaySound(SoundManager.Instance.slash);
                anim.Play("Obstacle_attack");
            }


        }

    }
}
